from .page import Page
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
#from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import *
from string import *


class InternalPage(Page):

    @property
    def logout_button(self):
        return self.driver.find_element_by_css_selector(
            "nav a[href $= '?logout']")

    @property
    def user_profile_link(self):
        return self.driver.find_element_by_css_selector(
            "nav a[href $= '?go=profile']")

    @property
    def user_management_link(self):
        return self.driver.find_element_by_css_selector(
            "nav a[href $= '?go=users']")

    @property
    def is_this_page(self):
        #print "Page Loaded!"
        return self.is_element_visible((By.CSS_SELECTOR, "div#results div"))

    @property
    def search_started(self):
        elem = self.driver.find_element_by_css_selector(
            "div#results").find_element_by_css_selector("div div")
        #print "Child Text ="+elem.text
        try:
            self.wait.until(staleness_of(elem))
            #print "Search Started!"
            return True
        except TimeoutException:  # WebDriverException:
            return False

    @property
    def home_link(self):
        if self.is_element_visible((By.XPATH, "//nav/ul/li/a")):
            return self.driver.find_element_by_xpath("//nav/ul/li/a")

    @property
    def film_name(self):
        return self.driver.find_element_by_css_selector("h2").text

    @property
    def add_movie(self):
        if self.is_element_visible((By.CSS_SELECTOR, "img[alt=\"Add movie\"]")):
            return self.driver.find_element_by_css_selector(
                "img[alt=\"Add movie\"]")
        else:
            self.home_link.click()

    @property
    def search_edit(self):
        return self.driver.find_element_by_xpath("//*[@id=\"q\"]")

    @property
    def get_expando(self):  # no more used
        jsn = str(self.driver.execute_script("return jQuery.expando"))
        return jsn

    @property
    def run_js(self, jsn):  # no more used
        try:
            self.wait.until(not(self.driver.execute_script(
                "return " + jsn + ".handle.elem.ajaxSearchLock")))
            hist = self.driver.execute_script(
                "return " + jsn + ".handle.elem.loadingTimer")
            return hist
        except TimeoutException:  # WebDriverException:
            print "Timeout Exception in JS wait!"
            return 0

    @property
    def films_list(self):
        return self.driver.find_elements_by_xpath(
            "//*[contains(@id, 'movie_')]")

    def delete_record(self, record, film):
        record.click()
        if self.is_element_visible((By.CSS_SELECTOR, "h2")):
            deleted_name = self.driver.find_element_by_css_selector("h2").text
        rate_text = replace(self.driver.find_element_by_css_selector(
            "div.rating").text, "Rating: ", "")
        if rate_text != "":
            rating = float(rate_text)
            print "Rating = " + str(rating) + " | ",#
        else:
            rating = 0
            print "Empty Rating | ",#
        if (rating < film.use_imdb) or (film.use_imdb == 0):
            self.driver.find_element_by_css_selector(
                "img[alt=\"Remove\"]").click()
            alert = self.driver.switch_to_alert()
            alert.accept()
            print "Film [" + deleted_name.encode(
                'cp866', errors='replace') + "] was deleted sucessfully"
            return False
        else:
            print "Film [" + deleted_name.encode(
                'cp866', errors='replace') + "] was not deleted"
            return True
